<?php
namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Lib\Response;
use App\Http\Model\Hint;
use App\Http\Services\UserServices;
use App\Http\Services\UnionServices;

class ApiUnionController extends BaseController
{
    protected $userService;
    protected $unionService;

    public function __construct(){
        $this->userService = new UserServices();
        $this->unionService = new UnionServices();
    }

    /**
     * 获取公会对公结算数据
     */
    public function getUnionSettleInfo(Request $request)
    {
        $user = $this->userService->get_current_user();
        if (empty($user)) {
            Response::returnResult(Hint::LOGIN_OUT);
            return;
        }
        $userId = $user['user_id'];
        $unionId = (int) $request->input("union_id");
        if (empty($unionId)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }
        $dateSt = $request->input("date_st");
        $dateSt = empty($dateSt) ? 0 : strtotime($dateSt);
        $dateEd = $request->input("date_ed");
        $dateEd = empty($dateEd) ? 0 : strtotime($dateEd);

        $data = $this->unionService->getUnionSettleInfo($userId, $unionId, $dateSt, $dateEd);
        if($data['code'] != Hint::SUCCESS) {
            Response::returnResult($data['code'], $data['data'], $data['msg']);
        }
        Response::returnResult(Hint::SUCCESS, $data['data']);
    }

    /**
     * 公会对公结算提交
     */
    public function submitUnionSettle(Request $request)
    {
        $user = $this->userService->get_current_user();
        if (empty($user)) {
            Response::returnResult(Hint::LOGIN_OUT);
            return;
        }
        $userId = $user['user_id'];
        $unionId = (int) $request->input("union_id");
        $id = (int) $request->input("id");
        $confirmImg = (string) $request->input("confirm_img");
        $invoiceImg = (string) $request->input("invoice_img");
        if (empty($unionId) || empty($id) || empty($confirmImg) || empty($invoiceImg)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }

        $data = $this->unionService->submitUnionSettle($userId, $unionId, $id, $confirmImg, $invoiceImg);
        if($data['code'] != Hint::SUCCESS) {
            Response::returnResult($data['code'], $data['data'], $data['msg']);
        }
        Response::returnResult(Hint::SUCCESS, $data['data']);
    }

    /**
     * 公会主播私信统计列表
     */
    public function getIdentityMsgStatList(Request $request)
    {
        $userId = $this->getLoginUserId();
        if(empty($userId)) {
            Response::returnResult(Hint::LOGIN_OUT);
        }
        $page = (int) $request->input('page_index', 0);
        $limit = (int) $request->input('page_size', 10);
        $timeSt = $request->input('time_st', 0);//开始时间戳
        $timeEd = $request->input('time_ed', 0);//结束时间戳
        $data = $this->unionService->getIdentityMsgStatList($page, $limit, $userId, $timeSt, $timeEd);

        Response::returnResult($data['code'], $data['data'], $data['msg']);
    }

    /**
     * 公会指定主播私信统计
     */
    public function getIdentityMsgStat(Request $request)
    {
        $userId = $this->getLoginUserId();
        if(empty($userId)) {
            Response::returnResult(Hint::LOGIN_OUT);
        }
        $timeSt = $request->input('time_st', 0);//开始时间戳
        $timeEd = $request->input('time_ed', 0);//结束时间戳
        $data = $this->unionService->getIdentityMsgStat($userId, $timeSt, $timeEd);

        Response::returnResult($data['code'], $data['data'], $data['msg']);
    }

    /**
     * 公会主播私信统计列表导出申请
     */
    public function addIdentityMsgStatExcel(Request $request)
    {
        $userId = $this->getLoginUserId();
        if(empty($userId)) {
            Response::returnResult(Hint::LOGIN_OUT);
        }
        $timeSt = $request->input('time_st', 0);//开始时间戳
        $timeEd = $request->input('time_ed', 0);//结束时间戳
        if (empty($timeSt) || empty($timeEd )) {
            Response::returnResult(Hint::PARAM_ERROR);
        }

        $data = $this->unionService->addIdentityMsgStatExcel($userId, $timeSt, $timeEd);

        Response::returnResult($data['code'], $data['data'], $data['msg']);
    }

    /**
     * 公会主播私信统计导出列表
     */
    public function getIdentityMsgStatExcelList(Request $request)
    {
        $userId = $this->getLoginUserId();
        if(empty($userId)) {
            Response::returnResult(Hint::LOGIN_OUT);
        }
        $page = (int) $request->input('page_index', 0);
        $limit = (int) $request->input('page_size', 10);
        $data = $this->unionService->getIdentityMsgStatExcelList($page, $limit, $userId);

        Response::returnResult($data['code'], $data['data'], $data['msg']);
    }





}
